package cn.enbug.j2ee2;

import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

/**
 * Created by zhuang on 2017/12/5.
 */
public class LoginListener implements HttpSessionAttributeListener {

    @Override
    public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {
        String username = httpSessionBindingEvent.getSession().getAttribute("username").toString();
        State.logins.putIfAbsent(username, "0");
        String times = State.logins.get(username);
        if (null == times) {
            State.logins.put(username, "0");
        } else {
            State.logins.put(username, String.valueOf(Integer.parseInt(times) + 1));
        }
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {

    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {

    }
}
